package my.learnDB.tools;

import my.learnDB.models.domain.Account;

import java.sql.*;
import java.util.ArrayList;

public class AAJDBC {

    // jdbc:mysql://localhost:3306/mylearn?&amp;useSSL=false&amp;serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=UTF-8"
    final static String url = "jdbc:mysql://localhost:3306/mylearn?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8";
    final static String user = "root";
    final static String password = "121388";

    public static void main(String[] args) throws SQLException, ClassNotFoundException {

        Class.forName("com.mysql.cj.jdbc.Driver");

        // 获取连接
        Connection conn = DriverManager.getConnection(url, user, password);

        // 准备SQL
        String sql = "select * from account where money > ?";

        // 获取SQL执行对象
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        // 参数：？的位置， 值
        preparedStatement.setFloat(1, 1000f);

        // 执行SQL获取结果
        ResultSet resultSet = preparedStatement.executeQuery();

        // 结果接封装
        ArrayList<Account> accountArrayList = new ArrayList<>();

        while (resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            float money = resultSet.getFloat("money");

            accountArrayList.add(new Account(id, name, money));
        }
        System.out.println("***********结果集数据*************");
        for (Account account : accountArrayList) {
            System.out.println(account);
        }

        resultSet.close();
        conn.close();
    }
}
